package cn.piesat.scanning.business.xz.service.forimpl;

import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildXIZANG_GPPServiceImpl implements BuildAlgorithmTaskService {
    
    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //草地碳汇与森林碳汇
        ///1000M/2022/20220730/FY3D_MERSI_GBAL_L1B_GLL_20220730_0700_1000M.NC
        Map<String, Object> params = new HashMap<>();
        params.put("primaryFile", primaryFile);
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String[] s1 = fileName.split("_");
        String startDate;
        String parameter07 = taskPathParam.getParameter07();
        if ("FY4A".equals(parameter07)) {
            startDate = s1[7];
        } else if ("LAD8".equals(parameter07) || "S2B".equals(parameter07) || "LAD9".equals(parameter07)) {
            startDate = s1[5] + s1[6];
        } else {
            startDate = s1[5] + s1[6] + "00";
        }
        HtScanningMetadataDTO metadataDTO = null;
        if ("S2B".equals(taskPathParam.getParameter07())) {
            metadataDTO = htScanningMetadataService.findMetaDataDto(startDate, taskPathParam.getParameter04(), taskPathParam.getParameter07(),
                    taskPathParam.getParameter05(), s1[2], "TIFF", "BAS");
        } else {
            metadataDTO = htScanningMetadataService.findMetaDataDto(startDate, taskPathParam.getParameter04(), taskPathParam.getParameter07(),
                    taskPathParam.getParameter05(), taskPathParam.getParameter06(), "TIFF", "BAS");
        }
        if (null == metadataDTO) {
            algorithmParams.put("parameter01", null);
        } else {
            params.put("l2File", taskPathParam.getParameter02() + metadataDTO.getFilePath());
            params.put("auxPath", taskPathParam.getParameter03());
            params.put("era5Path", "/mnt/sfs/auxdata/parm_forest/ERA5/");
            params.put("satelliteName", taskPathParam.getParameter07());
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        }
        return algorithmParams;
    }
}
